package com.sanron.sunweather.common;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class Config{
	private SharedPreferences sp;
	private boolean location;//是否自动获取位置
	private boolean updateOnStart;//进入程序是否立即更新数据
	private int updateSpan;//后台更新间隔 (单位分钟)
	private boolean isLocationPrimaryCity;//主城市是否是定位城市
	private LinkedList<String> cities;//用户添加的地区
	
	public static final int DEFAULT_UPDATE_SPAN = 30;
	public static final String SP_NAME = "config";
	
	public static Config instance;
	
	public static Config getInstance(Context context){
		if(instance==null){
			synchronized (Config.class) {
				if(instance==null){
					synchronized (Config.class) {
						instance = new Config(context.getApplicationContext());
					}
				}
			}
		}
		return instance;
	}
	
	private Config(Context context) {
		sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		location = sp.getBoolean("location", true);
		updateOnStart = sp.getBoolean("update_on_start", true);
		updateSpan = sp.getInt("update_pan",DEFAULT_UPDATE_SPAN);
		isLocationPrimaryCity = sp.getBoolean("is_location_primary",false);
		String jsonCities = sp.getString("cities",null);
		if(jsonCities!=null){
			cities = new Gson().fromJson(jsonCities,
					new TypeToken<LinkedList<String>>(){}.getType());
		}else{
			cities = new LinkedList<String>();
		}
	}

	
	public boolean isLocation() {
		return location;
	}

	public void setLocation(boolean location) {
		this.location = location;
		setValue("location", location);
	}


	public int getUpdateSpan() {
		return updateSpan;
	}

	public void setUpdateSpan(int updateSpan) {
		this.updateSpan = updateSpan;
		setValue("update_pan", location);
	}


	public boolean isUpdateOnStart() {
		return updateOnStart;
	}

	public void setUpdateOnStart(boolean updateOnStart) {
		this.updateOnStart = updateOnStart;
		setValue("update_on_start", updateOnStart);
	}

	public LinkedList<String> getCities() {
		return cities;
	}

	public void addCity(String city){
		this.cities.add(city);
		setValue("cities", new Gson().toJson(cities));
	}
	
	
	public void setPrimaryCity(String city){
		if(isLocationPrimaryCity){
			//如果当前主城市是定位的，则替换
			if(cities.size()>0){
				cities.set(0, city);
			}else{
				cities.add(city);
			}
		}else{
			//不是定位的，则把要设的主城市移动到0位置
			if(cities.contains(city)){
				cities.remove(city);
			}
			cities.addFirst(city);
		}
		setValue("cities", new Gson().toJson(cities));
	}
	
	@SuppressWarnings("unchecked")
	private void setValue(String name,Object value){
		Editor editor = sp.edit();
		if(value instanceof Integer){
			editor.putInt(name,(Integer)value);
		}else if(value instanceof Boolean){
			editor.putBoolean(name,(Boolean)value);
		}else if(value instanceof String){
			editor.putString(name,(String)value);
		}else if(value instanceof Float){
			editor.putFloat(name, (Float)value);
		}else if(value instanceof Long){
			editor.putLong(name, (Long)value);
		}else if(value instanceof Set<?>){
			editor.putStringSet(name, (Set<String>)value);
		}
		editor.commit();
	}

	public boolean isLocationPrimaryCity() {
		return isLocationPrimaryCity;
	}

	public void setLocationPrimaryCity(boolean isLocationPrimaryCity) {
		this.isLocationPrimaryCity = isLocationPrimaryCity;
		setValue("is_location_primary", isLocationPrimaryCity);
	}

}
